<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="霜序廿的个人网站">
  <link 
    rel="icon" 
    href="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg">
  <title>npm install模块安装机制</title>
  
    
      <meta 
        property="og:title" 
        content="npm install模块安装机制">
    
    
      <meta 
        property="og:url" 
        content="https://shuangxunian.github.io/2022/01/15/NPMInstallModuleInstallationMechanism/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg">
    
    
      <meta 
        property="og:img" 
        content="如题目">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2022-01-15">
      <meta 
        property="og:article:modified_time" 
        content="2022-01-15">
      <meta 
        property="og:article:author" 
        content="霜序廿">
      
        
          <meta 
            property="og:article:tag" 
            content="js">
        
          <meta 
            property="og:article:tag" 
            content="npm">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
      
      
      
      
        
        
        
        <script>
          function prismThemeChange() {
            if(document.getElementById('theme-color').dataset.mode === 'dark') {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism-tomorrow.min.css', '[data-prism]', 'prism-tomorrow');
              } else {
                loadCSS('/js/lib/prism/prism-tomorrow.min.css', 'prism', 'prism-tomorrow');
              }
            } else {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism.min.css', '[data-prism]', 'prism');
              } else {
                loadCSS('/js/lib/prism/prism.min.css', 'prism', 'prism');
              }
            }
          }
          prismThemeChange()
        </script>
      
      
        
        <link rel="stylesheet" href="/js/lib/prism/prism-line-numbers.min.css">
      
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
        prismThemeChange();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img" 
          src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">霜序廿的个人网站</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      npm install模块安装机制
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2022-01-15T11:29:28.346Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2022-01-15</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/" 
          class="post-meta-link">
          技术文章
        </a>
      
    
    
      <span class="dot"></span>
      <span>1.7k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/js/" 
            class="post-meta-link">
            js
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/tags/npm/" 
            class="post-meta-link">
            npm
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h2 id="为什么执行npm-install会更新package-lock-json？"><a href="#为什么执行npm-install会更新package-lock-json？" class="headerlink" title="为什么执行npm install会更新package-lock.json？"></a>为什么执行npm install会更新package-lock.json？</h2><p>你是否也有疑惑，为什么介入新项目初始化的时候，执行npm install安装依赖，项目的package-lock.json有时候会更新？package-lock.json就是用来固定依赖版本的，按理说依赖的版本都固定了，又没有安装新的依赖就不应该改变的，这是为什么？如果你也有这样的疑问，那么接下来可以进一步跟随我去了解npm install的模块安装机制。当然需要明确一下，这里提到的npm版本version 5及其之后的版本，因为在此之前，还未有package-lock.json之类的配置文件。</p>
<h2 id="npm-install（不带参数）安装机制"><a href="#npm-install（不带参数）安装机制" class="headerlink" title="npm install（不带参数）安装机制"></a>npm install（不带参数）安装机制</h2><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://api2.mubu.com/v3/document_image/dc1149d6-8da7-4176-982f-2db21f75ccd4-3807603.jpg" class="lozad post-image"src="https://api2.mubu.com/v3/document_image/dc1149d6-8da7-4176-982f-2db21f75ccd4-3807603.jpg"></p>
<h3 id="确定首层依赖"><a href="#确定首层依赖" class="headerlink" title="确定首层依赖"></a>确定首层依赖</h3><p>先检查package.json，通过dependencies和devDependencies属性中直接指定的模块。</p>
<h3 id="构建依赖树"><a href="#构建依赖树" class="headerlink" title="构建依赖树"></a>构建依赖树</h3><p>确定首层依赖后，会去构建依赖树。这是一个递归的过程，工程本身是整棵依赖树的根节点，每个首层依赖模块都是根节点下面的一棵子树，npm 会开启多进程从每个首层依赖模块开始逐步寻找更深层级的节点，形成依赖树。每个节点在递归的过程中会去确定节点模块的信息，比如版本、下载地址、压缩包地址等。</p>
<p>版本的确定机制：</p>
<p>package.json 中往往是 semantic version（semver，语义化版本）。此时如果版本描述文件（npm-shrinkwrap.json 或 package-lock.json或yarn.lock）中有该模块信息直接拿即可，如果没有则从远程仓库获取。如 packaeg.json 中某个包的版本是 ^1.0.0，npm 就会去仓库中获取符合 1.x.x 形式的最新版本。</p>
<h3 id="依赖树扁平化"><a href="#依赖树扁平化" class="headerlink" title="依赖树扁平化"></a>依赖树扁平化</h3><p>上一步得到的依赖树，一般是包含很多重复模块的，比如A模块依赖了moment，B模块也依赖了moment，npm在npm3之后进行了优化，不在严格按照依赖树来进行安装，因为这个过程会浪费大量资源和时间。做法就是对这颗树进行扁平化处理。即简单说来，它会遍历所有节点，逐个将模块放在根节点下面，也就是 node_modules 的第一层。当发现有重复模块时，则将其丢弃。</p>
<p>比如 node_modules 下 A 模块依赖 moment@^1.0.0，B 模块依赖 moment@^1.1.0，则 ^1.1.0 为兼容版本。</p>
<p>而当 A 模块依赖 moment@^2.0.0，B 模块依赖 moment@^1.1.0，则依据 semver 的规则，二者不存在兼容版本。会将一个版本放在 node_modules 中，另一个仍保留在依赖树里。</p>
<p>举个例子，假设一个依赖树原本是这样：</p>
<pre class="line-numbers language-none"><code class="language-none">node_modules
-- moduleA
---- moment@version1
-- moduleB
---- moment@version2<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>假设 version1 和 version2 是兼容版本，则经过 扁平化 会成为下面的形式：</p>
<pre class="line-numbers language-none"><code class="language-none">node_modules
-- moduleA
-- moduleB
-- moment（保留的版本为兼容版本）<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>假设 version1 和 version2 为非兼容版本，则后面的版本保留在依赖树中：</p>
<pre class="line-numbers language-none"><code class="language-none">node_modules
-- moduleA
-- moment@version1
-- moduleB
---- moment@version2<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="获取依赖树确定的模块"><a href="#获取依赖树确定的模块" class="headerlink" title="获取依赖树确定的模块"></a>获取依赖树确定的模块</h3><p>先会通过压缩包地址去判断是否在缓存中存在该版本模块，如果有，就直接拿过来，如果没有，就回去远程仓库下载，下载完后放入缓存，并解压放到node_modules目录中，最后会去新增或者更新lock文件。</p>
<h2 id="回答文章刚开始提出的问题"><a href="#回答文章刚开始提出的问题" class="headerlink" title="回答文章刚开始提出的问题"></a>回答文章刚开始提出的问题</h2><p>根据上面构建依赖树的过程，即npm install并不是完全按照lock文件去安装依赖的，是先通过package.json递归创建依赖，然后再看lock中对应的模块固定的版本是否符合semver规则，符合就直接确认版本安装，不符合就会按照package.json的指定的semver规则去仓库拉最新的版本信息并更新lock文件，外部条件不变的情况下（npm版本、上次构建代码等），理论上依赖版本不会发生变化，也就是lock不应该出现更新才对。那么究竟是什么导致了日常中我们感知到的lock变化了呢？</p>
<p>先明确一个机制：</p>
<pre class="line-numbers language-none"><code class="language-none">## package.json
&quot;dependencies&quot;: &#123;  
  &quot;core-js&quot;: &quot;~3.4.5&quot;  
&#125;
## package-lock.json
&quot;dependencies&quot;: &#123;  
  &quot;core-js&quot;: &#123;  
    &quot;version&quot;: &quot;3.4.7&quot;,  
    &quot;resolved&quot;: &quot;https:&#x2F;&#x2F;registry.npm.taobao.org&#x2F;core-js&#x2F;download&#x2F;core-js-3.4.7.tgz&quot;,  
    &quot;integrity&quot;: &quot;sha1-PdplYR2VaZtet3QupFHqBS03qmU&#x3D;&quot;  
  &#125;  
&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>如上示例代码，依赖的是core-js <del>3.4.5, 锁定的是3.4.7。 你把package.json里 core-js 的依赖改成 ~3.4.6 , ~3.4.7，重新安装都不会使 package-lock.json变化， 因为lock文件里面保存的版本比package文件里面的大。但是如果你把package.json文件里面的版本直接改成 “core-js”: “</del>3.4.8”, 这就比lock文件里面的版本要高了，需要重新下载最新的，会下载符合3.5.x 的最新版。同时更新lock文件。</p>
<p>上面这种情况属于小概率事件，但也不是不会发生的。即手动改了package.json的某个依赖的版本，但是没有去安装和自动更新lock文件，就提交上去了，那么你接手初始化项目，就可能出现lock文件更新的情况。</p>
<p>另外根据实践，还有一种可能，就是有些公司内部有自己的npm镜像，类似cnpm，使用这种类似cnpm install来安装依赖，一般不会更新lock文件，如果团队没统一安装依赖命令，有的用npm install 有的用类似cnpm install，package.json和lock的差异性就会增加，当你使用npm install时，就有可能会更新lock文件。</p>
<p>另外，最大可能的一种情况是大家使用的npm版本不一致，版本不同，那么组织生成lock文件的逻辑可能不同，就可能导致lock文件更新。</p>
<h2 id="npm-ci"><a href="#npm-ci" class="headerlink" title="npm ci"></a>npm ci</h2><p>之所以提这个命令，是因为开篇的那个问题，其实有一层含义就是提问者想要通过lock固定下来的版本安装工程，不想有任何版本变化，保持绝对一致。那么npm ci就是为此而生的。这个命令会完全按照工程的lock描述文件去安装依赖。要注意的是，当lock文件的描述版本不满足package.json依赖指定到semver规则时，会报错退出，并不会往下执行或者去更新lock文件。</p>
<h2 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h2><p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6999829253395054623">npm install模块安装机制</a></p>

  </div>
  <div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2022/01/15/JsToAchieveTheGenerationOfTextAvatar/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">js实现文字头像的生成 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2022/01/15/vueInterviewQuestions/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">vue面试题 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%89%A7%E8%A1%8Cnpm-install%E4%BC%9A%E6%9B%B4%E6%96%B0package-lock-json%EF%BC%9F"><span class="toc-text">为什么执行npm install会更新package-lock.json？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-install%EF%BC%88%E4%B8%8D%E5%B8%A6%E5%8F%82%E6%95%B0%EF%BC%89%E5%AE%89%E8%A3%85%E6%9C%BA%E5%88%B6"><span class="toc-text">npm install（不带参数）安装机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A1%AE%E5%AE%9A%E9%A6%96%E5%B1%82%E4%BE%9D%E8%B5%96"><span class="toc-text">确定首层依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%BE%9D%E8%B5%96%E6%A0%91"><span class="toc-text">构建依赖树</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%9D%E8%B5%96%E6%A0%91%E6%89%81%E5%B9%B3%E5%8C%96"><span class="toc-text">依赖树扁平化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E4%BE%9D%E8%B5%96%E6%A0%91%E7%A1%AE%E5%AE%9A%E7%9A%84%E6%A8%A1%E5%9D%97"><span class="toc-text">获取依赖树确定的模块</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9E%E7%AD%94%E6%96%87%E7%AB%A0%E5%88%9A%E5%BC%80%E5%A7%8B%E6%8F%90%E5%87%BA%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">回答文章刚开始提出的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-ci"><span class="toc-text">npm ci</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="toc-text">参考链接</span></a></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
    class="author-img" 
    alt="author avatar">

<p class="author-name">霜序廿</p>
<p class="author-description">无限进步</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>253</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>6</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>51</span>
    <span>标签</span>
  </a>
</div>

  <div class="author-card-society">
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://github.com/shuangxunian">
          <i class="iconfont icon-github society-icon"></i>
        </a>
      </div>
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://space.bilibili.com/391117803">
          <i class="iconfont icon-bilibili society-icon"></i>
        </a>
      </div>
    
  </div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%89%A7%E8%A1%8Cnpm-install%E4%BC%9A%E6%9B%B4%E6%96%B0package-lock-json%EF%BC%9F"><span class="toc-text">为什么执行npm install会更新package-lock.json？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-install%EF%BC%88%E4%B8%8D%E5%B8%A6%E5%8F%82%E6%95%B0%EF%BC%89%E5%AE%89%E8%A3%85%E6%9C%BA%E5%88%B6"><span class="toc-text">npm install（不带参数）安装机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A1%AE%E5%AE%9A%E9%A6%96%E5%B1%82%E4%BE%9D%E8%B5%96"><span class="toc-text">确定首层依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%BE%9D%E8%B5%96%E6%A0%91"><span class="toc-text">构建依赖树</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%9D%E8%B5%96%E6%A0%91%E6%89%81%E5%B9%B3%E5%8C%96"><span class="toc-text">依赖树扁平化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E4%BE%9D%E8%B5%96%E6%A0%91%E7%A1%AE%E5%AE%9A%E7%9A%84%E6%A8%A1%E5%9D%97"><span class="toc-text">获取依赖树确定的模块</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9E%E7%AD%94%E6%96%87%E7%AB%A0%E5%88%9A%E5%BC%80%E5%A7%8B%E6%8F%90%E5%87%BA%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">回答文章刚开始提出的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-ci"><span class="toc-text">npm ci</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="toc-text">参考链接</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/">
        <div class="categories-list-item">
          技术文章
          <span class="categories-list-item-badge">218</span>
        </div>
      </a>
    
      <a href="/categories/%E5%85%B6%E4%BB%96/">
        <div class="categories-list-item">
          其他
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/%E6%97%85%E6%B8%B8/">
        <div class="categories-list-item">
          旅游
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/%E7%AE%97%E6%B3%95/">
        <div class="categories-list-item">
          算法
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E8%80%83%E8%AF%95/">
        <div class="categories-list-item">
          考试
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E9%98%85%E8%AF%BB/">
        <div class="categories-list-item">
          阅读
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/js/" 
        title="js">
        <div class="tags-list-item">js</div>
      </a>
    
      <a 
        href="/tags/vue/" 
        title="vue">
        <div class="tags-list-item">vue</div>
      </a>
    
      <a 
        href="/tags/%E9%9D%A2%E8%AF%95/" 
        title="面试">
        <div class="tags-list-item">面试</div>
      </a>
    
      <a 
        href="/tags/css/" 
        title="css">
        <div class="tags-list-item">css</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C/" 
        title="网络">
        <div class="tags-list-item">网络</div>
      </a>
    
      <a 
        href="/tags/%E5%85%B6%E4%BB%96/" 
        title="其他">
        <div class="tags-list-item">其他</div>
      </a>
    
      <a 
        href="/tags/%E7%AE%97%E6%B3%95/" 
        title="算法">
        <div class="tags-list-item">算法</div>
      </a>
    
      <a 
        href="/tags/%E6%B5%8F%E8%A7%88%E5%99%A8/" 
        title="浏览器">
        <div class="tags-list-item">浏览器</div>
      </a>
    
      <a 
        href="/tags/html/" 
        title="html">
        <div class="tags-list-item">html</div>
      </a>
    
      <a 
        href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" 
        title="操作系统">
        <div class="tags-list-item">操作系统</div>
      </a>
    
      <a 
        href="/tags/%E8%80%83%E8%AF%95/" 
        title="考试">
        <div class="tags-list-item">考试</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AF%E5%AE%9E%E5%8A%9B/" 
        title="软实力">
        <div class="tags-list-item">软实力</div>
      </a>
    
      <a 
        href="/tags/DOM/" 
        title="DOM">
        <div class="tags-list-item">DOM</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AE%E5%AD%90/" 
        title="轮子">
        <div class="tags-list-item">轮子</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C%E5%8E%9F%E7%90%86/" 
        title="网络原理">
        <div class="tags-list-item">网络原理</div>
      </a>
    
      <a 
        href="/tags/debug/" 
        title="debug">
        <div class="tags-list-item">debug</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%89%A7%E8%A1%8Cnpm-install%E4%BC%9A%E6%9B%B4%E6%96%B0package-lock-json%EF%BC%9F"><span class="toc-text">为什么执行npm install会更新package-lock.json？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-install%EF%BC%88%E4%B8%8D%E5%B8%A6%E5%8F%82%E6%95%B0%EF%BC%89%E5%AE%89%E8%A3%85%E6%9C%BA%E5%88%B6"><span class="toc-text">npm install（不带参数）安装机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A1%AE%E5%AE%9A%E9%A6%96%E5%B1%82%E4%BE%9D%E8%B5%96"><span class="toc-text">确定首层依赖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%BE%9D%E8%B5%96%E6%A0%91"><span class="toc-text">构建依赖树</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%9D%E8%B5%96%E6%A0%91%E6%89%81%E5%B9%B3%E5%8C%96"><span class="toc-text">依赖树扁平化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E4%BE%9D%E8%B5%96%E6%A0%91%E7%A1%AE%E5%AE%9A%E7%9A%84%E6%A8%A1%E5%9D%97"><span class="toc-text">获取依赖树确定的模块</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9E%E7%AD%94%E6%96%87%E7%AB%A0%E5%88%9A%E5%BC%80%E5%A7%8B%E6%8F%90%E5%87%BA%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">回答文章刚开始提出的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#npm-ci"><span class="toc-text">npm ci</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="toc-text">参考链接</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-05-01</div>
        <a href="/2022/05/01/typescriptHome/"><div class="recent-posts-item-content">typescript</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptTheVariousWaysAndAdvantagesAndDisadvantagesOfDeepInheritance/"><div class="recent-posts-item-content">JavaScript深入之继承的多种方式和优缺点</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptGoFromPrototypeToPrototypeChain/"><div class="recent-posts-item-content">JavaScript深入之从原型到原型链</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptMemoryLeakTutorial/"><div class="recent-posts-item-content">JavaScript 内存泄漏教程</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    关注嘉然！顿顿解馋！
  </div>
  <div class="recent-posts-list">
    
      <img 
        src="https://api2.mubu.com/v3/document_image/2697c6ae-10ee-41a3-9099-304bdb252d31-3807603.jpg" 
        class="myadd-img" 
        alt="author avatar">
    
  </div>
</div>
  </article>
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2020 -
          
          2022
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          霜序廿的个人网站
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton" 
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
  </body>
</html>
